// pages/user/index.js
import { getUserInfo, getUserSongList, getCaptchaSent, checkLoginCaptcha, getUserSubcount, logoutAction, geteRegisterAnonimous } from '../../service/api_login'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneNumber: "",   // 用户手机号
    sms: "",   // 手机验证码
    phoneErrorMsg: "",  // 错误提示
    disabledSent: true, // 是否可以发送验证码
    disabledLogin: true, // 是否可以登录
    countDownNum: 0,    // 发送倒计时
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setStorageSync('userId', "350638140")
  },
  // 发送验证码
  handlesendCaptchaClick() {
    if (this.data.phoneNumber) {
      console.log(this.data.phoneNumber);
      // 17689482243
      getCaptchaSent(this.data.phoneNumber).then(res => {
        if (res.data.code === 200) {
          this.countDown()
        } else {
          wx.showToast({
            title: '发送错误',
            icon: "none",
            duration: 1500
          })
        }
      })
    }
  },
  // 监听手机号输入
  handlePhoneInput(event) {
    const phoneNumber = event.detail
    const phoneNumberRegExp = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
    const result = (phoneNumberRegExp.exec(phoneNumber));
    if (!result) {
      this.setData({ disabledSent: true })
    } else {
      this.setData({ phoneNumber, disabledSent: false })
    }
  },
  // 监听验证码输入
  handleSmsInput(event) {
    const sms = event.detail
    if (sms.length === 4) {
      this.setData({ sms, disabledLogin: false })
    }
  },
  // 游客登录
  handlegeteRegisterAnonimousClick() {
    geteRegisterAnonimous().then(res => {
      console.log(res);
      // const MUSIC_U=b604f28c0e677e478710c7673c221aa806bc636045486a4415b1c31b79f23855993166e004087dd33ad2ca37dab6e05548a2993e321e1483d42106b30fa748fa7759291272c260f0a89fe7c55eac81f3; 
    })
  },
  // 登录
  async handleLoginClick() {
    const res = await getUserInfo(this.data.phoneNumber, this.data.sms)
    console.log(res);
    if (res.data.code === 200) {
      const U = res.cookies.find(item => {
        item.indexOf('MUSIC_U') !== -1
      })
      // 保留用户信息
      wx.setStorageSync('token', res.data.token)
      wx.setStorageSync('cookie_key', U)
      wx.setStorageSync('userInfo', res.data.account)
      wx.setStorageSync('userId', "350638140")
      wx.navigateTo({
        url: '../detail-user/index',
      })
    } else {
      wx.showToast({
        title: "错误" + res.data.code + res.data.msg || res.data.message,
        duration: 3000,
        icon: "none"
      })
    }
  },
  handleLogoutClick() {
    logoutAction().then(res => {
      if (res.data.code === 200) {
        wx.showToast({
          title: '成功退出',
          icon: "none"
        })
      }
    })
  },
  handlejumpClick() {
    wx.navigateTo({
      url: '../detail-user/index',
    })
  },
  handleUserSubcountClick() {
    getUserSubcount().then((res) => {
      console.log(res);
    })

  },
  // 发送验证码倒计时
  countDown() {
    var countDownNum = 60 // 获取倒计时初始值
    var timer = setInterval(() => {
      countDownNum -= 1
      this.setData({ countDownNum })
      if (countDownNum <= 0) {
        clearInterval(timer)
      }
    }, 1000)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})